//
//  SportViewController.m
//  FitTime
//
//  Created by 赵建平 on 16/3/30.
//  Copyright © 2016年 赵建平. All rights reserved.
//

#import "SportViewController.h"
#import "SportView.h"
#import "SportSeletButtonCVCell.h"


@interface SportViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UIButton *selectButton;
@property (nonatomic, strong) SportView *popView;
@property (nonatomic, strong) SportView *downView;
@property (nonatomic, assign) BOOL popViewIsHiden;
@property (nonatomic, strong) NSArray *datasource;
@property (nonatomic, strong) NSArray *imageArray;
@property (nonatomic, strong) NSArray *colorArray;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSArray *startTitleArray;
@property (nonatomic, strong) UIButton *startButton;
@property (nonatomic, strong) UILabel *mileageLabel;  // 总里程label
@property (nonatomic, strong) UILabel *showMileageLabel;//显示总里程数据
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *showTimeLabel;
@property (nonatomic, strong) UILabel *countLabel;
@property (nonatomic, strong) UILabel *showCountLabel;

@end

@implementation SportViewController

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBarHidden = YES;
    self.navigationController.navigationBar.translucent = YES;
    self.view.backgroundColor = [UIColor colorWithRed:0.471 green:0.855 blue:0.318 alpha:1.000];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.datasource = @[@"runout-2.png", @"runin-2.png", @"walk-2.png", @"qixing-2.png", @"huaxue-2.png", @"huabing-2.png"];
    self.imageArray = @[@"runout.png", @"runin.png", @"walk.png", @"qixing.png", @"huaxue.png", @"huabing.png"];
    self.titleArray = @[@"户外跑步", @"室内跑步", @"走路", @"骑行", @"滑雪", @"滑冰"];
    self.startTitleArray = @[@"跑步", @"跑步", @"走路", @"骑行", @"滑雪", @"滑冰"];
    [self createSelectButton];
    [self createPopView];
    [self createCollectionView];
    [self createStartButton];
    [self createMileageLabel];
    [self createTimeLabel];
    [self createCountLabel];
    
    // Do any additional setup after loading the view from its nib.
}

#pragma mark - 右上角选择按钮
- (void)createSelectButton {
    
    self.selectButton = [UIButton buttonWithTitle:nil frame:CGRectMake(0, StatusBarHeight, 32, 32) target:self action:@selector(selectButtonAction)];
    [self.selectButton setBackgroundImage:[UIImage imageNamed:@"runout"] forState:UIControlStateNormal];
    [self.view addSubview:self.selectButton];
}
#pragma mark - 总里程label
- (void)createMileageLabel {
    
    self.mileageLabel = [UILabel labelWithFrame:CGRectZero color:[UIColor clearColor] alignment:NSTextAlignmentCenter];
    [self.view addSubview:self.mileageLabel];
    [self.mileageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.centerY.equalTo(self.view.mas_centerY).offset(-100);
        make.height.equalTo(@20);
    }];
    self.mileageLabel.text = @"跑步总里程(公里)";
    self.mileageLabel.textColor = [UIColor whiteColor];
    
    self.showMileageLabel = [UILabel labelWithFrame:CGRectZero color:[UIColor clearColor] alignment:NSTextAlignmentCenter];
    [self.view addSubview:self.showMileageLabel];
    [self.showMileageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.bottom.equalTo(self.mileageLabel.mas_bottom).offset(-20);
        make.height.equalTo(@100);

    }];
    self.showMileageLabel.text = @"0.00";
    self.showMileageLabel.font = [UIFont systemFontOfSize:70];
    self.showMileageLabel.textColor = [UIColor whiteColor];
    
}

#pragma mark - 总时间label
- (void)createTimeLabel {
    
    self.timeLabel = [UILabel labelWithFrame:CGRectZero color:[UIColor clearColor] alignment:NSTextAlignmentCenter];
    [self.view addSubview:self.timeLabel];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX).offset(-100);
        make.centerY.equalTo(self.view.mas_centerY);
        make.height.equalTo(@20);
    }];
    self.timeLabel.text = @"累计用时";
    self.timeLabel.textColor = [UIColor whiteColor];
    
    self.showTimeLabel = [UILabel labelWithFrame:CGRectZero color:[UIColor clearColor] alignment:NSTextAlignmentCenter];
    [self.view addSubview:self.showTimeLabel];
    [self.showTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.timeLabel.mas_centerX);
        make.bottom.equalTo(self.timeLabel.mas_bottom).offset(-20);
        make.height.equalTo(@50);
        
    }];
   
    NSString *string = @"0.0 小时";
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    NSRange range = [string rangeOfString:@"小时"];
    [attributedString setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} range:range];
    self.showTimeLabel.font = [UIFont systemFontOfSize:30];
    self.showTimeLabel.attributedText = attributedString;
    self.showTimeLabel.textColor = [UIColor whiteColor];
    
}
#pragma mark - 总次数label
- (void)createCountLabel {
    
    self.countLabel = [UILabel labelWithFrame:CGRectZero color:[UIColor clearColor] alignment:NSTextAlignmentCenter];
    [self.view addSubview:self.countLabel];
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX).offset(100);
        make.centerY.equalTo(self.view.mas_centerY);
        make.height.equalTo(@20);
    }];
    self.countLabel.text = @"运动次数";
    self.countLabel.textColor = [UIColor whiteColor];
    
    self.showCountLabel = [UILabel labelWithFrame:CGRectZero color:[UIColor clearColor] alignment:NSTextAlignmentCenter];
    [self.view addSubview:self.showCountLabel];
    [self.showCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.countLabel.mas_centerX);
        make.bottom.equalTo(self.countLabel.mas_bottom).offset(-20);
        make.height.equalTo(@50);
        
    }];
    
    NSString *str = @"0 次";
    NSMutableAttributedString *attributed = [[NSMutableAttributedString alloc] initWithString:str];
    NSRange range = [str rangeOfString:@"次"];
    [attributed setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} range:range];
    self.showCountLabel.font = [UIFont systemFontOfSize:30];
    self.showCountLabel.attributedText = attributed;
    self.showCountLabel.textColor = [UIColor whiteColor];
    
}

#pragma mark - 开始运动按钮
- (void)createStartButton {
    
    if (!self.startButton) {
        
        self.startButton = [UIButton buttonWithTitle:@"开始跑步" frame:CGRectZero target:self action:@selector(startButtonAction)];
        [self.view addSubview:self.startButton];
        
        [self.startButton mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.equalTo(self.view.mas_centerX);
            make.centerY.equalTo(self.view.mas_centerY).mas_equalTo(@100);
            make.width.equalTo(@50);
            make.height.equalTo(@50);
        }];
        self.startButton.layer.masksToBounds = YES;
        self.startButton.layer.cornerRadius = 25;
        self.startButton.titleLabel.numberOfLines=2;
        self.startButton.titleLabel.font = [UIFont systemFontOfSize:18];
        [self.startButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    
}

- (void)startButtonAction {
    
}

- (void)selectButtonAction {
    
    [UIView animateWithDuration:0.5f animations:^{
        self.popView.frame = CGRectMake(0, StatusBarHeight, SCREENWIDTH, SCREENHEIGHT - StatusBarHeight);
       
    }];
}

#pragma mark - 选择按钮------弹出视图
- (void)createPopView {
    
    self.popView = [[SportView alloc] initWithFrame:CGRectMake(0,SCREENHEIGHT, SCREENWIDTH, SCREENHEIGHT - StatusBarHeight)];
    self.popView.backgroundColor = [UIColor clearColor];
    [[UIApplication sharedApplication].keyWindow addSubview:self.popView];
    
    // 上方view
    SportView *topView = [[SportView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, self.popView.frame.size.height / 2)];
    topView.backgroundColor = [UIColor clearColor];
    [self.popView addSubview:topView];
    //  手势控制弹出和收起
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [topView addGestureRecognizer:tap];
    
    //  下方view
    self.downView = [[SportView alloc] initWithFrame:CGRectMake(0, self.popView.frame.size.height / 2, SCREENWIDTH, self.popView.frame.size.height / 2)];
    self.downView.backgroundColor = [UIColor whiteColor];
    [self.popView addSubview:self.downView];
    //  downView上布局
    UILabel *sportLabel = [UILabel labelWithFrame:CGRectZero color:[UIColor orangeColor] alignment:NSTextAlignmentCenter];
    [self.downView addSubview:sportLabel];
    [sportLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(self.downView.mas_centerX);
        make.top.equalTo(self.downView);
        make.left.equalTo(self.downView.mas_left);
        make.height.equalTo(@50);
    }];
    sportLabel.text = @"选择你的运动类型";
    sportLabel.font = [UIFont systemFontOfSize:20];
    //  关闭按钮
    UIButton *closeButton = [UIButton buttonWithTitle:@"guanbi.png" frame:CGRectZero target:self action:@selector(closeButtonAction)];
    [sportLabel addSubview:closeButton];
    closeButton.backgroundColor = [UIColor redColor];
    [sportLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(sportLabel.mas_centerY);
        make.right.equalTo(sportLabel.mas_right);
        make.width.equalTo(@32);
        make.height.equalTo(@32);
        
    }];
    
}

- (void)closeButtonAction {
    [UIView animateWithDuration:0.5f animations:^{
        self.popView.frame = CGRectMake(0, SCREENHEIGHT, SCREENWIDTH, SCREENHEIGHT - StatusBarHeight);
    }];
}

#pragma mark - 手势方法
- (void)tapAction:(UITapGestureRecognizer *)tap {
    
    [UIView animateWithDuration:0.5f animations:^{
        self.popView.frame = CGRectMake(0, SCREENHEIGHT, SCREENWIDTH, SCREENHEIGHT - StatusBarHeight);
    }];
}

- (void)createCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(SCREENWIDTH / 3,(self.downView.frame.size.height - 50) / 2);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, SCREENWIDTH, self.downView.frame.size.height - 50) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.downView addSubview:self.collectionView];
    [self.collectionView registerClass:[SportSeletButtonCVCell class] forCellWithReuseIdentifier:@"SportSeletButtonCVCell"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 6;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SportSeletButtonCVCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SportSeletButtonCVCell" forIndexPath:indexPath];
    cell.jpImageView.image = [UIImage imageNamed:self.imageArray[indexPath.item]];
    
    cell.titleLabel.text = self.titleArray[indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    //  开始按钮改变
    [self.startButton setTitle:[NSString stringWithFormat:@"开始%@", self.startTitleArray[indexPath.row]] forState:UIControlStateNormal];
    // 总里程 改变
    self.mileageLabel.text = [NSString stringWithFormat:@"%@总里程(公里)", self.startTitleArray[indexPath.row]];
    SportSeletButtonCVCell *cell = (SportSeletButtonCVCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
   
    cell.jpImageView.image = [UIImage imageNamed:self.datasource[indexPath.item]];
    // 替换选择按钮视图
    [self.selectButton setBackgroundImage:[UIImage imageNamed:self.imageArray[indexPath.item]] forState:UIControlStateNormal];
    
    // 收回视图
    [UIView animateWithDuration:0.5f animations:^{
        self.popView.frame = CGRectMake(0, SCREENHEIGHT, SCREENWIDTH, SCREENHEIGHT - StatusBarHeight);
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
